package iceberg.domain;

import iceberg.Config;

public final class Unit {

	public static enum Type {PACKAGE, CLASS}

	public static Unit createClassUnit(String name) {
		return new Unit(Type.CLASS, name);
	}

	public static Unit createPackageUnit(String name) {
		return new Unit(Type.PACKAGE, name);
	}

	private final Type type;
	private final String name;

	private Unit(Type type, String name) {
		if (type == null || name == null) {
			throw new IllegalArgumentException("Arguments can't be null");
		}
		this.type = type;
		this.name = name;
	}

	public boolean hasParent() {
		return name.contains(".");
	}

	public Unit getParent() {
		return new Unit(Type.PACKAGE, name.substring(0, name.lastIndexOf('.')));
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "[" + type + "::" + name + "]";
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || obj.getClass() != getClass()) {
			return false;
		}

		Unit other = (Unit) obj;
		return name.equals(other.name);// && type == other.type;
	}

	public static Unit createUnitFromRawFile(String path) {
		String location = Config.getTestProjectLocation();
		String fileName = path.substring(location.length() + 1, path.lastIndexOf('.')).replace('\\', '.');
		return createClassUnit(fileName);
	}

}
